﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

// Directive for the data model.
using LocalDatabase.Model;


namespace LocalDatabase.ViewModel
{
    public class FindMeFriendsViewModel : INotifyPropertyChanged
    {
        // LINQ to SQL data context for the local database.
        private FindMeDataContext FindMeDB;

        // Class constructor, create the data context object.
        public FindMeFriendsViewModel()
        {
            FindMeDB = new FindMeDataContext();
        }

        //
        // TODO: Add collections, list, and methods here.
        //

        // Write changes in the data context to the database.
        public void SaveChangesToDB()
        {
            FindMeDB.SubmitChanges();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        // All contacts.
        private ObservableCollection<FindMeFriend> _allFriends;
        public ObservableCollection<FindMeFriend> AllFriends
        {
            get { return _allFriends; }
            set
            {
                _allFriends = value;
                NotifyPropertyChanged("AllFriends");
            }
        }

        // Load all the contacts from the database
        public void LoadFriendsFromDatabase()
        {

            // Specify the query for all contacts in the database.
            var friendsInDB = from FindMeFriend friend in FindMeDB.FindMeFriends select friend;

            // Query the database and load all contacts.
           AllFriends = new ObservableCollection<FindMeFriend>(friendsInDB);

        }

        public int GetDatabaseCount()
        {
            var contactsInDB = (from FindMeFriend friend in FindMeDB.FindMeFriends select friend).Count();
            return contactsInDB;
        }

        public FindMeFriend GetContactsFromDatabase()
        {
            // Specify the query for all contacts in the database.
            var contactsInDB = (from FindMeFriend friend in FindMeDB.FindMeFriends select friend).FirstOrDefault();
            return contactsInDB;
        }

        // Add a contact to the database and collections.
        public void AddContact(FindMeFriend newContact)
        {
            // Add a contact to the data context.
            FindMeDB.FindMeFriends.InsertOnSubmit(newContact);

            // Save changes to the database.
            FindMeDB.SubmitChanges();

            // Add a contact to the "all" observable collection.
            AllFriends.Add(newContact);
        }

        // Remove a contact from the database and collections.
        public void DeleteContact(FindMeFriend contactForDelete)
        {

            // Remove the contact from the "all" observable collection.
            AllFriends.Remove(contactForDelete);

            // Remove the contact from the data context.
            FindMeDB.FindMeFriends.DeleteOnSubmit(contactForDelete);

            // Save changes to the database.
            FindMeDB.SubmitChanges();
        }
    }


    public class FavoriteLocationsViewModel : INotifyPropertyChanged
    {
        // LINQ to SQL data context for the local database.
        private FindMeDataContext db;

        // Class constructor, create the data context object.
        public FavoriteLocationsViewModel()
        {
            db = new FindMeDataContext();
        }

        // Write changes in the data context to the database.
        public void SaveChangesToDB()
        {
            db.SubmitChanges();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        private ObservableCollection<FavoriteLocation> _allLocations;
        public ObservableCollection<FavoriteLocation> AllLocations
        {
            get { return _allLocations; }
            set
            {
                _allLocations = value;
                NotifyPropertyChanged("AllLocations");
            }
        }

        public void LoadFavLocationsFromDatabase()
        {
            var favoriteLocationsInDB = from FavoriteLocation favLoc in db.FavoriteLocations select favLoc;

            AllLocations = new ObservableCollection<FavoriteLocation>(favoriteLocationsInDB);
        }

        public void AddLocation(FavoriteLocation newLocation)
        {
            var locations = from l in db.FavoriteLocations select l;
            newLocation.ID = locations.Count();

            db.FavoriteLocations.InsertOnSubmit(newLocation);

            db.SubmitChanges();

            AllLocations.Add(newLocation);
        }

        public void DeleteLocation(FavoriteLocation locationForDelete)
        {
            AllLocations.Remove(locationForDelete);

            db.FavoriteLocations.DeleteOnSubmit(locationForDelete);

            db.SubmitChanges();
        }
    }
}